var vue = new Vue({
    el: "#app",
    data() {
        return {
            id: '',
            place_id: '',
            check_info_id: '',
            project_id: '',
            check_id: '',
            dateShow: false, //时间弹框控制字段
            placeName: '', // 整改人
            phone: '', // 联系电话
            time: '', // 整改时间
            _time: '', // 整改时间
            radio: '', // 是否整改完毕
            fileList: [], //上传图片列表
            fileList2: [], //上传图片列表
            text: "", // 整改情况
        }
    },
    created() {
        this.id = getRequest().id || ''
        this.place_id = getRequest().place_id
        this.check_info_id = getRequest().check_info_id
        this.project_id = getRequest().project_id
        this.check_id = getRequest().check_id
        this.getUserInfo()
    },
    mounted() {

    },
    methods: {
        getUserInfo() {
            ajax({
                url: formal + '/person/info',
                type: 'GET',
                success: (res) => {
                    this.placeName = res.data.realname
                }
            })

        },
        async afterRead(file) {
            //组装数据得到，防止上传一张图片的时候不是数组
            let files = []
            if (!file.length) {
                files.push(file)
            } else {
                files = file
            }
            // 为新增的图片加上loading
            let _fLength = files.length
            let _length = this.fileList.length - _fLength
            // 首先判断上一次传输是否已经结束
            for (let i = 0; i < _length; i++) {
                if (this.fileList[i].status == 'uploading') {
                    vant.Toast('照片传输未结束，请稍等...')
                    // 去除本次上传的所有照片
                    this.fileList.splice(_fLength * (-1))
                    return;
                }
            }
            for (let i = 0; i < _fLength; i++) {
                this.fileList[_length + i].status = 'uploading'
                this.fileList[_length + i].message = '上传中'
            }
            // 记录上传失败的索引值，最后进行去除
            let indexErrArr = []
            // 开始循环上传
            for (let i = 0; i < _fLength; i++) {
                let data = {
                    image: files[i].content
                }
                const res = await this.upImage(data)
                if (res.status == 1) {
                    this.fileList[i + _length].status = 'done'
                    this.fileList[i + _length].content = res.data.url
                    this.fileList[i + _length].local = res.data.local

                } else {
                    vant.Toast(res.message)
                    indexErrArr.push(i + _length)
                }
            }
            // 去除失败的图片
            indexErrArr.forEach((item, index) => {
                this.fileList.splice(item - index, 1)
            })
            this.fileList2 = []
            this.fileList.forEach((item) => {
                this.fileList2.push(item.local)
            })
            console.log(indexErrArr);
        },
        upImage(data) {
            return new Promise((re, rj) => {
                $.ajax({
                    url: formal + '/files/image',
                    type: 'POST',
                    data: data,
                    success: (res) => {
                        re(res)
                    },
                    error: (err) => {
                        re(err)
                    }
                })
            })
        },
        // 删除照片
        deleteImage() {
            this.fileList2 = [];
            this.fileList.forEach((item) => {
                this.fileList2.push(item.local)
            })
        },
        // 返回
        goBack() {
            window.history.go(-1);
        },
        // 时间选择器确定事件
        dateTimeConfirm(e) {
            let time = new Date(e)
            let t = time.getTime()
            let year = time.getFullYear()
            let mon = time.getMonth() + 1
            mon = mon > 9 ? mon : '0' + mon
            let day = time.getDate()
            day = day > 9 ? day : '0' + day
            let h = time.getHours()
            h = h > 9 ? h : '0' + h
            let min = time.getMinutes()
            min = min > 9 ? min : '0' + min
            let s = time.getSeconds()
            s = s > 9 ? s : '0' + s
            let str = year + '-' + mon + '-' + day + ' ' + h + ':' + min + ':' + s
            this.time = str
            this._time = t / 1000
            this.dateShow = false
        },
        // 提交
        set() {
            const reg =
                /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;

            if (!this.placeName) {
                vant.Toast(`请填写整改人姓名`);
                return
            } else if (!reg.test(this.phone)) {
                vant.Toast(`请填写正确手机号`);
                return
            } else if (this.time == '') {
                vant.Toast(`请选择整改时间`);
                return
            } else if (this.radio == '') {
                vant.Toast(`请选择是否整改完毕`);
                return
            } else if (this.fileList2.length == 0) {
                vant.Toast(`请上传整改后照片`);
                return
            } else if (!this.text) {
                vant.Toast(`请填写整改情况`);
                return
            }

            let data = {
                id: this.id,
                place_id: this.place_id,
                check_id: this.check_id,
                check_info_id: this.check_info_id,
                project_id: this.project_id,
                is_ok: this.radio,
                realname: this.placeName,
                phone: this.phone,
                time: this._time,
                content: this.text,
                image_list: this.fileList2,
            }
            console.log(data);
            ajax({
                url: formal + '/place-check/create-check-rectify',
                type: "POST", //默认get
                data: data,
                success: (res) => { //请求成功回调
                    vant.Toast({
                        type: 'success',
                        message: '提交成功',
                    });
                    setTimeout(() => {
                        history.back()
                    }, 500)
                }
            })
        }
    }
})